package com.xywy.bloodandroid.act;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.MD5;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.PostRequest;

/**
 * 意见反馈
 */
public class FeedBackActivity extends BaseActivity {

	private TextView middle_title; // title
	private RelativeLayout left_img; // left button
	private EditText et_send_content; // get feed back content
	private TextView right_title; // right button
	private TextView tv_count_down;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.activity_feed_back);

		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("意见反馈");
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		et_send_content = (EditText) findViewById(R.id.et_send_content);
		right_title = (TextView) findViewById(R.id.right_title);
		right_title.setText("发送");
		right_title.setVisibility(View.VISIBLE);
		tv_count_down = (TextView) findViewById(R.id.tv_count_down);
		tv_count_down.setMaxEms(600);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		right_title.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		et_send_content.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				String str = et_send_content.getText().toString().length()
						+ "/600";
				tv_count_down.setText(str);

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			FeedBackActivity.this.finish();
			break;

		case R.id.right_title:
			String content = et_send_content.getText().toString().trim();
			if (content == null || content.length() < 1) {
				showToast("您还没有写建议呢～");
			} else {
				submit(content);
			}
			break;
		}
	}

	Handler handler = new Handler();

	private void submit(String content) {
		// TODO Auto-generated method stub
		String sign = null;
		try {
			sign = MD5.md5s(Constants.Sign + Constants.tag);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		String url = "http://api.wws.xywy.com/index.php?" + "&tag="
				+ Constants.tag + "&sign=" + sign + "&act=" + "kbb" + "&fun="
				+ "user_feedback";
		PostRequest<String> postRequest = new PostRequest<String>(url,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						Toast.makeText(context, "提交失败！", Toast.LENGTH_SHORT)
								.show();

					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(context, waittingDialog);
						if (response != null) {
							JSONObject jsonObject;
							try {
								jsonObject = new JSONObject(response);
								String state = jsonObject.getString("state");
								if (state.equals("200")) {
									Toast.makeText(context, "提交成功！",
											Toast.LENGTH_SHORT).show();
									finish();
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}

						}
					}
				});

		HashMap<String, String> params = new HashMap<String, String>();
		params.put("userid", User.getInstance(this).getUserid());// userid
		params.put("apptype", "10004");// apptype
		try {
			params.put("content", URLEncoder.encode(content, "UTF-8")
					.toString());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		postRequest.setIsParseJson(false);
		postRequest.setParams(params);
		executeRequest(postRequest);

	}

}
